package com.minelx.datamicroscope.core.query;

import io.gitee.minelx.filesystem.File;
import com.minelx.statementsplus.cha.base.ChaSession;
import com.minelx.statementsplus.cha.base.TaskCommitment;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class SingleQueryJob {

	private final ChaSession session;

	private final TaskCommitment taskCommitment;

	private final String representTitle;

	public SingleQueryJob(ChaSession session, TaskCommitment taskCommitment, String representTitle) {
		this.session = session;
		this.taskCommitment = taskCommitment;
		this.representTitle = representTitle;
	}

	// FIXME 2022/3/11 needed test cover.
	public void work(File destination) {
		session.download(taskCommitment, destination);
		if (destination.empty()) { // file is not exists
			try {
				destination.write(representTitle, StandardCharsets.UTF_8);
			} catch (IOException e) {
				throw new RuntimeException("error while writing csv content.", e);
			}
		}
	}
}
